<?php

namespace common\models\demand;

use Yii;

/**
 * This is the model class for table "{{%demand_stats}}".
 *
 * @property int $id
 * @property int $demand_id
 * @property int $views
 * @property int $likes
 * @property int $collects
 * @property int $comments
 * @property int $created_at
 * @property int $updated_at
 */
class DemandStats extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%demand_stats}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['demand_id', 'views', 'likes', 'collects', 'comments', 'created_at', 'updated_at'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'demand_id' => 'Article ID',
            'views' => 'Views',
            'likes' => 'Likes',
            'collects' => 'Collects',
            'comments' => 'Comments',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'views',
            'likes',
            'collects',
            'comments',
        ];
    }

    public static function growUpViews($id) {
        $model = static::findOrNew([
            'demand_id' => $id,
        ]);
        $model->views += 1;
        return $model->save();
    }

}
